package com.panda.pandawash.util;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;

import com.panda.pandawash.api.ApplicationConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huangyusheng on 17-3-17.
 * check permission
 */

public class PermissionUtils {

    private static int hasLocationPermission;
    private static int hasStoragePermission;
    private static int hasCameraPermission;

    public static boolean locationPermission = false;
    public static boolean storagePermission = false;
    public static boolean cameraPermission = false;




    @TargetApi(23)
    public static void locationCheck(Activity activity){
        hasLocationPermission = activity.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);
        List<String> permissions = new ArrayList<String>();
        if(hasLocationPermission!= PackageManager.PERMISSION_GRANTED){
            permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
            permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }else{
            locationPermission=true;
        }
        if( !permissions.isEmpty() ) {
            activity.requestPermissions(permissions.toArray(new String[permissions.size()]), ApplicationConstant.REQUEST_CODE_SOME_FEATURES_PERMISSIONS);
        }
    }

    @TargetApi(23)
    public static void storageCheck(Activity activity){
        hasStoragePermission = activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        List<String> permissions = new ArrayList<String>();
        if(hasStoragePermission!= PackageManager.PERMISSION_GRANTED){
            permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }else{
            storagePermission=true;
        }
        if( !permissions.isEmpty() ) {
            activity.requestPermissions(permissions.toArray(new String[permissions.size()]), ApplicationConstant.REQUEST_CODE_SOME_FEATURES_PERMISSIONS);
        }
    }

    // Gps是否可用
    public static boolean isLocationEnable(final Context context) {
        LocationManager locationManager
                = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }

    @TargetApi(23)
    public static void cameraCheck(Activity activity){
        hasCameraPermission = activity.checkSelfPermission(Manifest.permission.CAMERA);
        List<String> permissions = new ArrayList<String>();
        if(hasCameraPermission!= PackageManager.PERMISSION_GRANTED){
            permissions.add(Manifest.permission.CAMERA);
        }else{
            cameraPermission=true;
        }
        if( !permissions.isEmpty() ) {
            activity.requestPermissions(permissions.toArray(new String[permissions.size()]), ApplicationConstant.REQUEST_CODE_SOME_FEATURES_PERMISSIONS);
        }
    }


}
